package com.jilefojing.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.forexpose.StatusEnumForExpose;
import com.jilefojing.beans.enums.forexpose.TypeEnumForExpose;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.Expose;
import com.jilefojing.beans.pojos.enhanced.EnhancedExpose;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.beans.query.ExposeQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IExposeDao;
import com.jilefojing.service.IExposeService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月24日 22时00分28秒
 */
@Service
public class ExposeService extends BaseService<Expose> implements IExposeService {
	@Autowired
	private IExposeDao exposeDao;

	@Override
	public Expose add(Expose expose, UserContext userContext) throws UserContextException {
		ItemTypeEnumForSource itemType = ItemTypeEnumForSource.getItemTypeEnumByCode(expose.getItemType());
		if (itemType == null) {
			throw new UserContextException("[不支持的举报对象类型]");
		}
		TypeEnumForExpose type = TypeEnumForExpose.getTypeEnumByCode(expose.getType());
		if (type == null) {
			type = TypeEnumForExpose.OTHER;
		}
		if (type == TypeEnumForExpose.OTHER && Detect.notEmpty(expose.getContent())) {
			throw new UserContextException("[举报内容不能为空]");
		}
		if (type == TypeEnumForExpose.OTHER && expose.getContent().length() > 200) {
			throw new UserContextException("[举报内容不能超过200字]");
		}

		List<EnhancedExpose> enhancedExposes = this.getEnhancedExposesByItemForUser(itemType, expose.getItemId(), userContext);
		if (Detect.notEmpty(enhancedExposes)) {
			throw new UserContextException("[您已经在" + new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(enhancedExposes.get(0).getCreateTime()) + "举报了,请耐心等待处理]");
		}
		Expose record = new Expose();
		record.setItemId(expose.getItemId());
		record.setItemType(itemType.getCode());
		record.setType(type.getCode());
		if (type == TypeEnumForExpose.OTHER) {
			record.setContent(expose.getContent());
		} else {
			record.setContent(type.getMsg());
		}
		record.setUserId(userContext.getUserId());
		Date now = new Date();
		record.setCreateTime(now);
		record.setUpdateTime(now);
		record.setStatus(StatusEnumForExpose.PENDING_TREATMENT.getCode());

		return exposeDao.saveSelective(record);
	}

	@Override
	public List<EnhancedExpose> getEnhancedExposesByItemForUser(ItemTypeEnumForSource itemType, long itemId, UserContext userContext) {
		return this.getEnhancedExposesByItemForUser(itemType, Arrays.asList(new Long[] { itemId }), userContext);
	}

	@Override
	public List<EnhancedExpose> getEnhancedExposesByItemForUser(ItemTypeEnumForSource itemType, List<Long> itemIds, UserContext userContext) {
		ExposeQuery exposeQuery = new ExposeQuery();
		exposeQuery.setItemIds(itemIds);
		exposeQuery.setItemTypes(Arrays.asList(new ItemTypeEnumForSource[] { itemType }));
		exposeQuery.setUserIds(Arrays.asList(new Long[] { userContext.getUserId() }));

		Pagination<EnhancedExpose> pagination = new Pagination<EnhancedExpose>(1, Integer.MAX_VALUE, false);
		pagination = this.getPagination(exposeQuery, pagination, userContext);
		if (!Detect.notEmpty(pagination.getItems())) {
			return null;
		}
		List<EnhancedExpose> enhancedExposes = pagination.getItems();
		return enhancedExposes;
	}

	@Override
	public List<EnhancedExpose> appendEnhancedCommons(List<? extends BaseEnhanced> enhancedItems, UserContext userContext) {
		// TODO Auto-generated method stub
		return (List<EnhancedExpose>) enhancedItems;
	}

	@Override
	public Pagination<EnhancedExpose> getPagination(ExposeQuery exposeQuery, Pagination<EnhancedExpose> pagination, UserContext userContext) {
		@SuppressWarnings("unchecked")
		Pagination<Expose> exposePagination = exposeDao.getPagination(exposeQuery, pagination.copy());
		pagination.setCount(exposePagination.getCount());
		if (!Detect.notEmpty(exposePagination.getItems())) {
			return pagination;
		}
		pagination.setItems(this.conventExposes2EnhancedExposes(exposePagination.getItems()));
		this.appendEnhancedCommons(pagination.getItems(), userContext);
		return pagination;
	}

	@Override
	public List<EnhancedExpose> conventExposes2EnhancedExposes(List<Expose> exposes) {
		List<EnhancedExpose> enhancedExposes = new ArrayList<EnhancedExpose>();
		for (Expose _Expose : exposes) {
			enhancedExposes.add(new EnhancedExpose(_Expose));
		}
		return enhancedExposes;
	}

}
